package com.feeyin.radar.msg.controller;

import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.model.InternalMailbox;
import com.feeyin.radar.msg.service.InternalMailboxService;
import com.feeyin.radar.sys.model.Department;
import com.feeyin.radar.sys.model.Users;
import com.feeyin.radar.sys.service.DepartmentService;
import com.feeyin.radar.sys.service.UserService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class InternalMailboxController extends MultiActionController
{
  private InternalMailboxService internalMailboxService;
  private UserService userService;
  private DepartmentService departmentService;
  private String internalMailbox;
  private String internalInbox;
  private String internalOutbox;
  private String internalWastebox;
  private String addInternalMail;
  private String viewInternalMail;
  private String selectPerson;

  public InternalMailboxService getInternalMailboxService()
  {
    return this.internalMailboxService;
  }

  public void setInternalMailboxService(InternalMailboxService internalMailboxService)
  {
    this.internalMailboxService = internalMailboxService;
  }

  public UserService getUserService() {
    return this.userService;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public DepartmentService getDepartmentService() {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public String getInternalMailbox() {
    return this.internalMailbox;
  }

  public void setInternalMailbox(String internalMailbox) {
    this.internalMailbox = internalMailbox;
  }

  public String getInternalInbox() {
    return this.internalInbox;
  }

  public void setInternalInbox(String internalInbox) {
    this.internalInbox = internalInbox;
  }

  public String getInternalOutbox() {
    return this.internalOutbox;
  }

  public void setInternalOutbox(String internalOutbox) {
    this.internalOutbox = internalOutbox;
  }

  public String getInternalWastebox() {
    return this.internalWastebox;
  }

  public void setInternalWastebox(String internalWastebox) {
    this.internalWastebox = internalWastebox;
  }

  public String getAddInternalMail() {
    return this.addInternalMail;
  }

  public void setAddInternalMail(String addInternalMail) {
    this.addInternalMail = addInternalMail;
  }

  public String getViewInternalMail() {
    return this.viewInternalMail;
  }

  public void setViewInternalMail(String viewInternalMail) {
    this.viewInternalMail = viewInternalMail;
  }

  public String getSelectPerson() {
    return this.selectPerson;
  }

  public void setSelectPerson(String selectPerson) {
    this.selectPerson = selectPerson;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("sign", request.getParameter("sign"));
    model.put("result", request.getParameter("result"));
    return new ModelAndView(getInternalMailbox(), model);
  }

  public ModelAndView internalInbox(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sFlag = request.getParameter("sFlag");
    String subject = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      subject = request.getParameter("subject");
    }
    model.put("sFlag", sFlag);
    model.put("subject", subject);

    Page page = new Page();
    page.getPageList(this.internalMailboxService.getPageInternalMailboxsCount(userId, 0, subject).intValue(), null, request);
    model.put("mails", this.internalMailboxService.getPageInternalMailboxs(userId, page, 0, subject));
    return new ModelAndView(getInternalInbox(), model);
  }

  public ModelAndView internalOutbox(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sFlag = request.getParameter("sFlag");
    String subject = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      subject = request.getParameter("subject");
    }
    model.put("sFlag", sFlag);
    model.put("subject", subject);

    Page page = new Page();
    page.getPageList(this.internalMailboxService.getPageInternalMailboxsCount(userId, 1, subject).intValue(), null, request);
    model.put("mails", this.internalMailboxService.getPageInternalMailboxs(userId, page, 1, subject));
    return new ModelAndView(getInternalOutbox(), model);
  }

  public ModelAndView internalWastebox(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sFlag = request.getParameter("sFlag");
    String subject = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      subject = request.getParameter("subject");
    }
    model.put("sFlag", sFlag);
    model.put("subject", subject);

    Page page = new Page();
    page.getPageList(this.internalMailboxService.getPageInternalMailboxsCount(userId, 2, subject).intValue(), null, request);
    model.put("mails", this.internalMailboxService.getPageInternalMailboxs(userId, page, 2, subject));
    return new ModelAndView(getInternalWastebox(), model);
  }

  public ModelAndView selectPerson(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    List users = new ArrayList();
    users = this.userService.getActivityUsers();
    if (users != null) {
      for (int i = 0; i < users.size(); i++) {
        Users user = new Users();
        user = (Users)users.get(i);
        if (user.getDeptId() != null) {
          Department department = new Department();
          department = this.departmentService.getDepartment(user.getDeptId());
          if (department != null) {
            user.setDeptName(department.getDeptName());
          }
        }
      }
    }

    model.put("users", users);
    return new ModelAndView(getSelectPerson(), model);
  }

  public ModelAndView preAddInternalMail(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("sign", request.getParameter("sign"));
    return new ModelAndView(getAddInternalMail(), model);
  }

  public ModelAndView addInternalMail(HttpServletRequest request, HttpServletResponse response, InternalMailbox internalMailbox)
  {
    boolean result = false;
    int success = 0;
    String sign = request.getParameter("sign");
    if (internalMailbox != null) {
      result = this.internalMailboxService.insertInternalMail(internalMailbox);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./internalMailbox.do?action=display&result=" + success + "&sign=" + sign));
  }

  public ModelAndView moveInternalMailToWastebox(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String sign = request.getParameter("sign");
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String[] ids = request.getParameterValues("id");
    if ((this.internalMailbox != null) && (!StringUtils.isEmpty(sign))) {
      result = this.internalMailboxService.moveInternalMailToWastebox(ids, userId, Integer.parseInt(sign));
      if (result) success = 2;
    }
    return new ModelAndView(new RedirectView("./internalMailbox.do?action=display&result=" + success + "&sign=" + sign));
  }

  public ModelAndView deleteInternalMail(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String sign = request.getParameter("sign");
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String[] ids = request.getParameterValues("id");
    if ((this.internalMailbox != null) && (!StringUtils.isEmpty(sign))) {
      result = this.internalMailboxService.deleteInternalMail(ids, userId, Integer.parseInt(sign));
      if (result) success = 3;
    }
    return new ModelAndView(new RedirectView("./internalMailbox.do?action=display&result=" + success + "&sign=" + sign));
  }

  public ModelAndView viewInternalMail(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    String sign = request.getParameter("sign");
    model.put("sign", sign);
    String id = request.getParameter("id");
    if ((!StringUtils.isEmpty(id)) && (!StringUtils.isEmpty(sign))) {
      model.put("internalMailbox", this.internalMailboxService.getInternalMailById(Integer.valueOf(Integer.parseInt(id))));
      if ("1".equals(sign)) {
        this.internalMailboxService.updateInternalMailForReaded(Integer.valueOf(Integer.parseInt(id)));
      }
    }
    return new ModelAndView(getViewInternalMail(), model);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
  {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
  }
}